package com.xiaohao.ctdemo.service;

import com.mathworks.toolbox.javabuilder.MWException;
import com.xiaohao.ctdemo.bean.Medicine;
import com.xiaohao.ctdemo.dao.MedicineDao;
import drug.Drug;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author 小浩
 * @date 2018-08-27 17:01
 * @package com.xiaohao.ctdemo.service
 * @description
 */

@Service
public class DrugService {

    @Autowired
    MedicineDao medicineDao;

    @Cacheable(value = "drug")
    public List drug(String name) throws IOException, MWException {
        Drug drug = new Drug();
        List<Object> list = new LinkedList<>();

        Medicine byName = medicineDao.findByName(name);

        if (byName == null) {
            throw new IOException();
        }

        Object[] result = drug.main(2, byName.getId());
        list.add(String.valueOf(result[0]));

        for (Object o : result) {
            System.out.println(result);
        }

        String numStr = String.valueOf(result[1]);

        System.out.println(numStr);
        String[] replace = numStr.split("    ");
        List<Integer> id = new LinkedList<>();

        for (int i = 0; i < replace.length; i++) {
            System.out.println(replace[i]);
            id.add(Integer.valueOf(replace[i].replace(" ", "")));
        }

        System.out.println(id);
        List<Medicine> medicinesById = medicineDao.findMedicinesByIdIn(id);
        list.addAll(medicinesById);
        System.out.println(medicinesById);
        return list;
    }
}
